package com.hhl.flowlayoutdemo.slice;

import com.hhl.flowlayoutdemo.ResourceTable;
import com.hhl.library.LogUtil;
import com.hhl.library.OnInitSelectedPosition;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HanHailong on 15/10/19.
 */
public class TagAdapter<T> extends BaseItemProvider implements OnInitSelectedPosition {

  private final Context mContext;
  private final List<T> mDataList;

  public TagAdapter(Context context) {
    this.mContext = context;
    mDataList = new ArrayList<>();
  }

  @Override
  public int getCount() {
    return mDataList.size();
  }

  @Override
  public Object getItem(int position) {
    return mDataList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public Component getComponent(int i, Component component, ComponentContainer componentContainer) {

    final Component cpt;
    if (component == null) {
      cpt = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_tag_item, null, false);
    } else {
      cpt = component;
    }

    Button textView = (Button) cpt.findComponentById(ResourceTable.Id_tv_tag);
    T t = mDataList.get(i);
    if (t instanceof String) {
      textView.setText((String) t);
    }
    cpt.setPressState(true);
    LogUtil.debug("msz", "cpt: " + cpt.isSelected());
    cpt.setBackground(new ShapeElement(mContext, ResourceTable.Graphic_bg_label_unchecked));
    return cpt;
  }

  public static void setItem(Component childView) {

  }

  public void onlyAddAll(List<T> datas) {
    mDataList.addAll(datas);
    notifyDataChanged();
  }

  public void clearAndAddAll(List<T> datas) {
    mDataList.clear();
    onlyAddAll(datas);
  }

  @Override
  public boolean isSelectedPosition(int position) {
    if (position % 2 == 0) {
      return true;
    }
    return false;
  }
}
